<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定义子父组件并给子组件传递props</title>
</head>

<body>
    <div id="app"></div>

    <script src="../../00_课程资料/js/react.development.js"></script>
    <script src="../../00_课程资料/js/react-dom.development.js"></script>
    <script src="../../00_课程资料/js/babel.min.js"></script>

    <script type="text/babel">
        class A extends React.Component{
            state = {
                cart:'奔驰'
            }

            changeCart = ()=>{
                this.setState({
                    cart:'奥拓'
                })
            }

            render(){
                return(
                    <div>
                        <button onClick={this.changeCart}>点击换车</button>  
                        {/* 在父组件中直接写子组件的名称即可表示父子组件关系 */}
                        <B cart={this.state.cart}/>  
                    </div>
                )
            }
        }

        class B extends React.Component{
            // 组件将要接收新的props时触发
            componentWillReceiveProps(props){
                console.log(props);
                console.log('组件将要接收新的props时触发 - componentWillReceiveProps');
            }
            // 是否更新
            shouldComponentUpdate(){
                console.log('是否更新:True');
                return true
            }
            // 将要更新
            componentWillUpdate(){
                console.log('将要更新');
            }
            // 编译
            render(){
                console.log('编译');
                return(
                    <h3>当前的车辆是：{this.props.cart}</h3>
                )
            }
            componentDidUpdate(){
                console.log("更新完成");
            }
        }

        ReactDOM.render(<A/>,document.getElementById('app'))
    </script>
</body>

</html>